package cn.youyeku.zsxh.ui.activity.ABS;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.ui.UIControl;
import cn.youyeku.zsxh.adapter.RefreshFootAdapter;
import cn.youyeku.zsxh.api.ResponseStatus;
import cn.youyeku.zsxh.app;


/**
 * Created by wings on 2016/9/20.
 */
public abstract class AbsSwipeActivity<T> extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, ResponseStatus {


    RecyclerView mRcyView;
    protected List<T> mData = new ArrayList<>();
    RefreshFootAdapter<T> mRefreshAdapter;
    LinearLayoutManager layoutManager;
    SwipeRefreshLayout mSwipeRefreshLayout;

    protected int mRcyLayoutView;
    protected int mRcyId;
    protected int mRcySwipId;

    private int cfgPreLoadingNum = 0; //

    protected abstract RefreshFootAdapter<T> getAapter();


    protected abstract void requestData();

    protected Handler handler = new Handler() {

        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
        }
       // 将会在主线程
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case STATUS_SUCCESS:

                    mRefreshAdapter.addMoreItem(mData);
                    //
                    mRefreshAdapter.changeStatus(mRefreshAdapter.STATUS_NOT_MORE_DATA);
                    break;
                case STATUS_NOT_MORE_DATA:
                    mRefreshAdapter.changeStatus(mRefreshAdapter.STATUS_NOT_MORE_DATA);
                    break;
                case STATUS_NO_LOGIN:
                    app.backgroundThreadShortToast("你还没有登录");
                    UIControl.showLogin(getApplicationContext());
                    break;
                case STATUS_GUOQI:
                    app.backgroundThreadShortToast("你还没有登录");
                    UIControl.showLogin(getApplicationContext());
                    break;
            }

            mSwipeRefreshLayout.setRefreshing(false);
        }
    };


    /**

     * @param mRcyId         recycle的id
     * @param mRcySwipId     下拉刷新的id

     */
    public AbsSwipeActivity(int mRcyId, int mRcySwipId) {


        this.mRcyId = mRcyId;
        this.mRcySwipId = mRcySwipId;


    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {


        mRcyView = (RecyclerView) findViewById(mRcyId);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(mRcySwipId);


        mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light, android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        mSwipeRefreshLayout.setProgressViewOffset(false, 0, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
                        .getDisplayMetrics()));

        mSwipeRefreshLayout.setOnRefreshListener(this);
        //setting layout manage
        setLayoutV_H(LinearLayoutManager.VERTICAL);

        mRefreshAdapter = getAapter();

        onRefresh();


        mRcyView.setAdapter(mRefreshAdapter);


        mRcyView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                int totalItemCount = layoutManager.getItemCount();
                int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
                if (lastVisibleItem >= totalItemCount - cfgPreLoadingNum && lastVisibleItem <20) {
                    LogUtils.w(lastVisibleItem + "前面可视化item   全部的item " + totalItemCount +" 预读"+ cfgPreLoadingNum);

                    onRefresh();

                }
            }
        });

        super.onCreate(savedInstanceState);
    }




    public void setLayoutV_H(int type) {
        layoutManager = new LinearLayoutManager(getApplicationContext());
        layoutManager.setOrientation(type);
        mRcyView.setLayoutManager(layoutManager);
    }

    @Override
    public void onRefresh() {
        mRefreshAdapter.changeStatus(mRefreshAdapter.STATUS_LOADING);

        requestData();


    }




}
